package com.hiwan.system.web.login;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hiwan.platform.utils.EncryptUtil;
import com.hiwan.platform.utils.StringUtils;
import com.hiwan.system.cache.OrgCacheService;
import com.hiwan.system.common.base.BaseController;
import com.hiwan.system.common.exception.ResourceNotFoundException;
import com.hiwan.system.dao.org.OrgDao;
import com.hiwan.system.dao.user.UserDao;
import com.hiwan.system.pojo.MenuInfo;
import com.hiwan.system.pojo.OrgInfo;
import com.hiwan.system.pojo.RoleInfo;
import com.hiwan.system.pojo.UserInfo;
import com.hiwan.system.service.menu.MenuService;
import com.hiwan.system.service.user.UserService;
import com.hiwan.system.util.Base64;
import com.hiwan.system.util.DictCacheUtils;
import com.hiwan.system.util.PrivateKeyReader;

@Controller
public class LoginController extends BaseController{
	
	@Autowired
	private MenuService menuService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private OrgCacheService orgCacheService;
	@Autowired
	private OrgDao orgDao;
	
	@RequestMapping("login")
	public String login(String menuId,@ModelAttribute User second, HttpSession session,HttpServletRequest request, ModelMap model) {
		Subject subject = SecurityUtils.getSubject();
		Object principal = subject.getPrincipal();
		if(principal != null){
			if (StringUtils.isEmpty(menuId)) {
				return "login/frame";
			}else{
				String menuUrl;
				if (StringUtils.equals(menuId, "00000")) {
					return "login/main";
				}else{
					
					MenuInfo menuInfo = menuService.getMenuInfo(menuId);
					if (menuInfo == null) {
						throw new ResourceNotFoundException("未知的菜单：" + menuId);
					}
					session.setAttribute("menuId", menuId);
					menuUrl = menuInfo.getMenuUrl();
					
				}
				return "redirect:/" + menuUrl;
			}
			
		}else{
			return "security/login";
		}
	}
	
	@RequestMapping("/mobile/loginMob.do")
	@ResponseBody
	public Map<String,Object> loginMobile(ModelMap model, String username, String password) {
		Map<String,Object> map = new HashMap<String,Object>();
		UserInfo user = userService.getUserInfo(username);
		if(user != null){
			byte[] bpassword = Base64.decode(password.toCharArray());
			try {
				password = PrivateKeyReader.decryptByPrivateKey(bpassword);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (!EncryptUtil.validatePassword(password, user.getUserPwd())) {
				map.put("status", "false");
				map.put("msg", "用户ID或密码错误, 请重试.");
				return map;
			}
			if (!StringUtils.equals(user.getStatus(), "1")) {
				String status = DictCacheUtils.getDictValue(user.getStatus(), "D00026");
				map.put("status", "false");
				map.put("msg", "该帐号的状态为【"+ status + "】，禁止登录.");
				return map;
			}
			
			OrgInfo tempO = orgDao.selectOrgInfo(user.getOrgId());
			if(StringUtils.equals(tempO.getStatus(), "2")){
				String status = DictCacheUtils.getDictValue(tempO.getStatus(), "D00027");
				map.put("status", "false");
				map.put("msg", "该帐号的状态为【"+ status + "】，禁止登录.");
				return map;
			}
			UserInfo u = new UserInfo(username);
			List<RoleInfo> roles = userDao.selectUserRoles(u);
			if(roles == null || roles.isEmpty()){
				map.put("status", "false");
				map.put("msg", "该用户未分配任何可用角色权限.");
				return map;
			}
			u.setUserName(user.getUserName());
			u.setOrgId(user.getOrgId());
			u.setOrgName(orgCacheService.getOrgName(user.getOrgId()));
			map.put("status", "true");
			map.put("user", user);
		}else{
			map.put("status", "false");
			map.put("msg", "用户ID或密码错误, 请重试.");
		}
		return map;
	}
	
	@RequestMapping("login/404Error")
	public String handerError(){
		return "common/404";
	}
}
